<template>
  <div class="main">
    <el-card class="box-card">
      <Form ref="form" :formobj="formobj" :formdata="formData" :formrules="formrules" @changelanmu="changelanmu" />
      <div style="padding-left:100px">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
import Form from '@/components/form.vue'
import { contentApiAdd, columnqueryAll, contentApiedit, contentApiqueryOne } from '@/api/webpage'
import { validateNum } from '@/utils/validator'
export default {
  name: 'Addform',
  components: {
    Form
  },
  data() {
    return {
      options: [],
      formData: {
        columnId: '',
        ctitle: '',
        title: '',
        no: '',
        content: '',
        url: '',
        fileUrl: '',
        signBy: '',
        createTime: '',
        type: 'article'
      },
      formobj: [
        { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
        { label: '标题', key: 'title', type: 'input' },
       /*  { label: '作者', key: 'author', type: 'input' },
        { label: '审核人', key: 'postedBy', type: 'input' }, */
        { label: '发布时间', key: 'createTime', type: 'date' },
      ],
      formrules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        no: [
          { required: false, message: '请输入序号', trigger: 'blur' },
          {
            validator: validateNum,
            message: '请输入正整数',
            trigger: 'blur'
          }
        ],
        signBy: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        postedBy: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.$route.query.cid) { // 如果是点击栏目进来的  就需要禁用选择
      this.formData.columnId = this.$route.query.cid
      this.changeForm(this.$route.query.cid)
      this.formobj[0].disabled = true
    } else {
      this.getlist()
      this.formobj[0].disabled = false
    }

    if (this.$route.query.id) {
      this.getdata(this.$route.query.id)
    }
  },
  methods: {
    changeForm(val) {
      this.getlist()
    },
    changelanmu(val) {
      this.changeForm(val)
    },
    getdata(id) {
      contentApiqueryOne({ id: id }).then(res => {
        if (res.code == 0) {
          this.formData = res.data
        }
        // 设置新闻栏目
        // this.changeForm(this.formData.columnId)
        console.log('新闻数据：', this.formData)
      })
    },
    getlist() {
      this.options = []
      this.formobj[0].options = []
      const data = {
        name: '',
        code: '',
        parentId: ''
      }
      columnqueryAll(data).then((res) => {
        if (res.code == 0) {
          if (this.$route.query.cid) {
            res.data.map(item => {
              this.options.push({ label: item.name, value: item.id })
              this.formobj[0].options.push({ label: item.name, value: item.id })
            })
          } else {
            res.data.map(item => {
              if (item.name != '友情链接' && item.name != '审核管理' &&
                item.name != '精彩视频' &&
                item.name != '视频点播' &&
                // 政策法规 不包含政策法规栏目
                item.name.indexOf('政策法规') == -1 &&
                item.name != '轮播新闻' &&
                item.name != '图文链接管理' &&
                item.name != '今日排班' &&
                item.name != '排版模板' &&
                item.name != '下载中心' &&
                item.name != '精彩瞬间' &&
                item.name != '值班人员' &&
                item.name != '办公入口' &&
                item.name != '各地动态' &&
                // item.name != '机关动态' &&
                item.name != '支队概况' &&
                // item.name != '防火工作' &&
                // item.name != '救援工作' &&
                // item.name != '政治工作' &&
                // item.name != '后勤工作' &&
                // item.name != '纪检工作' &&
                // item.name != '综合工作' &&
                item.name != '法规一' &&
                item.name != '法规二' &&
                item.name != '视频插件' &&
                item.name != '省内消防系统' &&
                item.name != '常用字体' &&
                item.name != '安全杀毒' &&
                item.name != '消防站训练计划' &&
                item.name != '安全杀毒吉林省消防救援总队' &&
                item.name != '国家消防救援局' &&
                item.name != '吉林省消防救援总队' &&
                item.name != '办公软件' &&
                item.name != '统计信息' &&
                item.name != '通讯录') {
                //  验证各地动态栏目
                if (
                  item.id == '437C9EAB57FB4C558A33EC96445F7487' ||
                  item.id == '21BCBF2C36DF4175A22296F3EE3D069F' ||
                  item.id == '6AEBF34D0F9D44D6A769A75D9CBB0F50' ||
                  item.id == '94D8ADCCF64640E9B986A38380B9DAA9' ||
                  item.id == '52BB3EDFD6FA47A891191F2B43FE643D' ||
                  item.id == '45F7E806AF264307BC0F823EB1657919'
                ) {
                  item.name = '各地动态—' + item.name
                }
                this.options.push({ label: item.name, value: item.id })
                this.formobj[0].options.push({ label: item.name, value: item.id })
              }
            })
          }
        }
      })
    },
    handleCurrentChange(val) {
      this.params.page = val
    },
    submit() {
      this.$refs.form.getData().then(res => {
        // return console.log("addform数据",res)
        if (Array.isArray(res.fileUrl)) {
          let fileurl = []
          let filename = []
          fileurl = res.fileUrl.map(item => {
            console.log(item)
            if (item.response) {
              if (item.response.data[0].address.includes('\\')) {
                return item.response.data[0].address.replace('\\', '/')
              } else {
                return item.response.data[0].address
              }
            } else {
              return item.url
            }
          })
          filename = res.fileUrl.map(item => {
            return item.name
          })
          res.fileUrl = fileurl.join(',')
          res.fileName = filename.join(',')
        }
        // 验证是增加还是编辑
        if (this.$route.query.type == 'edit') {
          contentApiedit(res).then((response) => {
            if (response.data == 1) {
              this.$message.success('成功')
              this.$router.go(-1)
            } else {
              this.$message.error('失败')
            }
          })
            .catch((err) => {
              console.log(err)
            })
        } else {
          res.type = 'article'
          res.approval = '1'
          console.log('添加新闻数据是：', res)
          contentApiAdd(res).then((response) => {
            if (response.data == 1) {
              this.$message.success('成功')
              // this.$router.go(-1)
              this.$router.push('/zbrjlist/index')
            } else {
              this.$message.error('失败')
            }
          })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>@import "~@/assets/css/adminstyle.css";</style>
